//
//  RefreshControl.swift
//  Weibo
//
//  Created by 李振兴 on 15/10/23.
//  Copyright © 2015年 李振兴. All rights reserved.
//

import UIKit
private let WBRefreshControlOffset: CGFloat = -60
class WBRefreshControl: UIRefreshControl {
    override func beginRefreshing() {
        super.beginRefreshing()
        refreshView.startAnimation()
    }
    override func endRefreshing() {
        super.endRefreshing()
        
        refreshView.endAnimation()
    }
    //kvo监听方法
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if frame.origin.y > 0 {
            return
        }
        if refreshing {
            refreshView.startAnimation()
            return
        }
        if frame.origin.y < WBRefreshControlOffset && !refreshView.rotateFlag {
            
            refreshView.rotateFlag = true
        }else if frame.origin.y > WBRefreshControlOffset && refreshView.rotateFlag {
            
            refreshView.rotateFlag = false
        }
    }
    override init() {
        super.init()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    private func setupUI(){
        
        addSubview(refreshView)
        refreshView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
            make.size.equalTo(refreshView.bounds.size)
        }
        // 方法触发会在下一次运行循环开始！
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            
            self.addObserver(self, forKeyPath: "frame", options: [], context: nil)
        }
    }
    deinit{
        self.removeObserver(self, forKeyPath: "frame")
    }
    private lazy var refreshView:WBRefreshView = WBRefreshView.refreshView()
}
class WBRefreshView:UIView {
    @IBOutlet weak var loadingIconView: UIImageView!
    @IBOutlet weak var tipView: UIView!
    @IBOutlet weak var tipIconView: UIImageView!
    var rotateFlag = false{
        didSet{
            rorateTipIcon()
        }
    }
    func rorateTipIcon(){
        UIView.animateWithDuration(0.5) { () -> Void in
            var angle = CGFloat(M_PI)
            angle += self.rotateFlag ? -0.0000001 : 0.0000001
            self.tipIconView.transform = CGAffineTransformRotate(self.tipIconView.transform, angle)
        }
    }
    //动画
    func startAnimation(){
        tipView.hidden = true
        if loadingIconView.layer.animationForKey("load") == nil{
            
            let ani = CABasicAnimation(keyPath: "transform.rotation")
            ani.toValue = 2 * M_PI
            ani.repeatCount = MAXFLOAT
            ani.duration = 0.5
            ani.removedOnCompletion = false
            loadingIconView.layer.addAnimation(ani, forKey: "load")
        }
        
        
    }
    func endAnimation(){
        tipView.hidden = false
        loadingIconView.layer.removeAllAnimations()
        
    }
    class func refreshView()->WBRefreshView{
//       return NSBundle.mainBundle().loadNibNamed("WBRefreshView", owner: nil, options: nil).last as! WBRefreshView
        let nib = UINib(nibName: "WBRefreshView", bundle: nil)
        return nib.instantiateWithOwner(nil, options: nil).last as! WBRefreshView
    }
}

